<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cart;
use App\Order;
use App\Item;

class ShopController extends Controller
{
     protected $gs  = [
          1=>['goods_id'=>1,'goods_name'=>'白百合 清香宜人', 'price'=>23.1], 
          2=>['goods_id'=>2,'goods_name'=>'红玫瑰 热烈奔放', 'price'=>23.2], 
          3=>['goods_id'=>3,'goods_name'=>'黄牡丹 雍容华贵', 'price'=>23.3], 
          4=>['goods_id'=>4,'goods_name'=>'狗尾巴 淡泊名利', 'price'=>23.4], 
       ];


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    { 
      /*  $sess = $request->session();
       // print_r($sess->get('user'));
        if(!$sess->has('user')){
           return redirect('login');
          }
        echo 'index';*/
     return view('index');
    }
    
   public function goods($id){

    $goods = $this->gs[$id];

     return view('goods', ['goods'=>$goods]);
      
    }

   public function buy($id){
     
    $goods = $this->gs[$id];
   
    Cart::add($goods['goods_id'], $goods['goods_name'], $goods['price'], 1, []);
  
    return redirect('cart');

   }

  public function cart(){
  
    $goods = Cart::getContent();
    
    $tot = Cart::getTotal();
    return view('cart',['goods'=>$goods, 'tot'=>$tot]); 
  
  }


 public function done(Request $request){
    $order = new Order();
    $order->ordsn = date('Ymd').mt_rand(10000,999999);
    $order->xm = $request->xm;
    $order->tel = $request->tel;
    $order->adress = $request->adress;
    $order->money = Cart::getTotal();
    $order->ispay = 0;
    $order->ordtime = time();

    $order->save();

    foreach(Cart::getContent() as $i){
       $item = new Item();
       $item->oid = $order->oid;
       $item->gid = $i->id;
       $item->goods_name = $i->name;
       $item->price = $i->price;
       $item->amount = $i->quantity;
       
       $item->save();

     }

      return view('done',['money'=>$order->money]);
  }

}
